package mashibing.class14;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 最多能有多少个会议能用
 *
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-05-25
 */
public class Class14_3_BestArrange {

    public static class Program {
        public int start;
        public int end;

        public Program(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }

    public static int bestArrange1(Program[] programs) {
        if (programs == null || programs.length == 0) {
            return 0;
        }
        return process(programs,0,0);
    }

    public static int process(Program[] programs, int done, int endLine) {
        if(programs == null) {
            return done;
        }
        int max = done;
        for (int i = 0; i < programs.length; i++) {
            if (programs[i].start >= endLine) {
                Program[] next = removeProgramByIndex(programs, i);
                int ans = process(next, done + 1, programs[i].end);
                max = Math.max(max, ans);
            }
        }
        return max;
    }

    public static Program[] removeProgramByIndex(Program[] programs, int removeIndex) {
        Program[] copyPrograms = new Program[programs.length - 1];
        int index = 0;
        for (int i = 0; i < programs.length; i++) {
            if (i != removeIndex) {
                copyPrograms[index++] = programs[i];
            }
        }
        return copyPrograms;
    }

    // 贪心算法：按最早结束的会议排序
    public static int bestArrange2(Program[] programs) {
        if (programs == null || programs.length == 0) {
            return 0;
        }
        Arrays.sort(programs, new BestArrangeComparator());
        int done = 0;
        int endLine = 0;
        for (int i = 0; i < programs.length; i++) {
            if(programs[i].start >= endLine) {
                done++;
                endLine = programs[i].end;
            }
        }
        return done;
    }

    public static class BestArrangeComparator implements Comparator<Program> {

        @Override
        public int compare(Program o1, Program o2) {
            return o1.end - o2.end;
        }
    }

    public static void main(String[] args) {
        int maxSize = 10;
        int maxValue = 20;
        int testCount = 10000;
        boolean flag = true;
        System.out.println("begin!");
        for (int i = 0; i < testCount; i++) {
            Program[] programs1 = generateProgramsArray(maxSize,maxValue);
            Program[] programs2 = copyProgramsArray(programs1);
            int ans1 = bestArrange1(programs1);
            int ans2 = bestArrange2(programs2);
            if(ans1 != ans2) {
                for (int i1 = 0; i1 < programs1.length; i1++) {
                    System.out.print(programs1[i1].start + "-" + programs1[i1].end + ", ");
                }
                System.out.println();
                flag = false;
            }
        }
        if(flag) {
            System.out.println("success!");
        }
    }

    // for test
    private static Program[] generateProgramsArray(int maxSize, int maxValue) {
        Program[] programs = new Program[(int) (Math.random() * maxSize + 1)];
        for (int i = 0; i < programs.length; i++) {
            int num1 = (int) (Math.random() * maxValue);
            int num2 = (int) (Math.random() * maxValue);
            if(num1 == num2) {
                programs[i] = new Program(num1,num2 + 1);
            } else {
                programs[i] = new Program(Math.min(num1,num2),Math.max(num1,num2));
            }
        }
        return programs;
    }
    // for test
    private static Program[] copyProgramsArray(Program[] programs) {
        Program[] copy = new Program[programs.length];
        for (int i = 0; i < programs.length; i++) {
            copy[i] = programs[i];
        }
        return copy;
    }

}
